﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: ClassifierBase.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-12-08 8:26 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using BOL.Linq;
using BOL.Maths.Statistics;

namespace BOL.Algorithms.Classification
{
    public abstract class ClassifierBase<TFeature, TClass> : IClassifier<TFeature, TClass>
    {
        #region Public methods

        public abstract TClass Classify(TFeature[] features);

        public IEnumerable<TClass> Classify<TSource>(IEnumerable<TSource> sources, Func<TSource, TFeature[]> featuresSelector)
        {
            if (sources == null)
                throw new ArgumentNullException("sources");
            if (featuresSelector == null)
                throw new ArgumentNullException("featuresSelector");

            return sources.Select(featuresSelector).Select(Classify);
        }

        public ClassificationMeasures Classify<TSource>(IEnumerable<TSource> sources, Func<TSource, TClass> classSelector, Func<TSource, TFeature[]> featuresSelector, TClass trueValue)
        {
            if (sources == null)
                throw new ArgumentNullException("sources");
            if (classSelector == null)
                throw new ArgumentNullException("classSelector");
            if (featuresSelector == null)
                throw new ArgumentNullException("featuresSelector");

            var list = sources.ToList();
            var actual = list.Select(classSelector);
            var expected = Classify(list, featuresSelector);

            return actual.ConfusionMatrix(expected, trueValue);
        }

        #endregion
    }
}